package systemFile;

import general.ActionEnum;
import general.FileUtils;
import general.GlobalProperties;
import org.apache.commons.vfs2.FileChangeEvent;
import org.apache.commons.vfs2.FileListener;
import org.apache.commons.vfs2.FileType;
import org.apache.log4j.Logger;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;

public class FileWatcher implements FileListener {
    private static Logger log = Logger.getLogger(FileWatcher.class);
    public static TrayIcon icon;
    private File lastModifyFile = new File(GlobalProperties.getFolderMonit() + "maincatalog.properties");

    public FileWatcher(TrayIcon icon) {
        FileWatcher.icon = icon;
        icon.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                openExplolerWindow();
            }
        });
    }

    @Override
    public void fileChanged(FileChangeEvent event) throws Exception {
        if (GlobalProperties.isLogged) {
            String rootFolderName = event.getFile().getParent().getName().getBaseName();
            // Jezeli folder jest profilem
            if (GlobalProperties.getProfiles().contains(rootFolderName)) {

                if (event.getFile().getType() == FileType.FOLDER) {
                    // folderow nie synchronizujemy
                }

                if (event.getFile().getType() == FileType.FILE) {
                    // wyslij
                    lastModifyFile = new File(event.getFile().getURL().getFile());
                    // pomijamy properties -> wysyłane dopiero po modyfikacji
                    if (!FileUtils.getExtension(lastModifyFile).equals("properties")) {
                        icon.displayMessage("", "Plik " + FileUtils.getFileName(event.getFile().getURL()) + " został zmieniony.", TrayIcon.MessageType.INFO);
                        FileBuffer.getInstance().getListOfFile().put(lastModifyFile, ActionEnum.EDIT);
                        FileUtils.dopiszZmianyDoProperties(event, rootFolderName);
                    }
                }
            }
        }
    }

    @Override
    public void fileCreated(FileChangeEvent event) throws Exception {
        if (GlobalProperties.isLogged) {
            String rootFolderName = event.getFile().getParent().getName().getBaseName();
            // Jezeli folder jest profilem
            if (GlobalProperties.getProfiles().contains(rootFolderName)) {
                if (event.getFile().getType() == FileType.FOLDER) {
                    // folderow nie synchronizujemy
                }
                if (event.getFile().getType() == FileType.FILE) {
                    lastModifyFile = new File(event.getFile().getURL().getFile());
                    icon.displayMessage("", "Plik " + FileUtils.getFileName(event.getFile().getURL()) + " został stworzony.", TrayIcon.MessageType.INFO);
                    FileBuffer.getInstance().getListOfFile().put(lastModifyFile, ActionEnum.ADD);
                }
            }
        }
    }

    @Override
    public void fileDeleted(FileChangeEvent event) throws Exception {
        if (GlobalProperties.isLogged) {
            String rootFolderName = event.getFile().getParent().getName().getBaseName();
            // Jezeli folder jest profilem
            if (GlobalProperties.getProfiles().contains(rootFolderName)) {
                if (event.getFile().getType() == FileType.IMAGINARY) {
                    lastModifyFile = new File(event.getFile().getURL().getFile());
                    icon.displayMessage("", "Plik " + FileUtils.getFileName(
                            event.getFile().getURL()) + " został usuniety.", TrayIcon.MessageType.INFO);
                    FileBuffer.getInstance().getListOfFile().put(lastModifyFile, ActionEnum.DELETE);
                }
            }
        }
    }

    private void openExplolerWindow() {
        Desktop desktop = null;
        if (Desktop.isDesktopSupported()) {
            desktop = Desktop.getDesktop();
        } else {
            System.err.println("OS doesn't support that action!");
            System.exit(1);
        }
        try {
            desktop.open(lastModifyFile.getParentFile());
        } catch (IOException e) {
            log.error(e.getMessage());
        }
    }
}
